from random import randint 

otherdice = {}



## craps ##

gameison = False
comes = [4,5,6,8,9,10]
craps = [2,3,12]
natural = [7, 11]

## 4,5,6,8,9,10
betcome = {4 : 0, 5 : 0, 6 : 0, 8 : 0, 9 : 0, 10 : 0 }
backcome = {4 : 0, 5 : 0, 6 : 0, 8 : 0, 9 : 0, 10 : 0 }
# comemax should be replace by a array like the one above
# so there can be specific max and min bet for each come box
comelinemax = 70
comelinetotal = 0

comebox = 0
betpass = 0
backpass = 0

betmin = 5
balance = 1000

onnum = 0

def bet_pass():
	global betpass,balance
	if betpass == 0:
		print "betting pass"
		betpass = betmin
		balance -= betmin

def bet_comebox():
	global comebox,comelinetotal,balance
	#print ">>%d" % comebox
	if comebox == 0 and comelinemax > comelinetotal:
		print "betting in comebox"
		comebox = betmin 
		balance -= betmin
def bet_come(tot): 
	global comebox,betcome,balance,comelinetotal
	# go = game one, true
	if comebox != 0:
		print "moving come bet to %d" % tot
		if betcome[tot] == 0:
			print "placing odds on come bet"
			back_come(tot)
		betcome[tot] = comebox
		comelinetotal += comebox
		comebox = 0
	

def back_pass():
	global backpass,balance
	# go = game one, true
	# only back pass once
	if backpass == 0:
		print "backing passline bet"
		backpass = betmin * 2 
		balance -= betmin * 2

def back_come(tot):
	global backcome,balance, betcome, comelinetotal
	backcome[tot] = betmin * 2
	comelinetotal += betmin * 2
	balance -= betmin * 2 

def crapsout():
	global betcome,backcome,betpass,backpass,onnum,comelinetotal
	print "you lost $%d" % ( sum(betcome.values()) + sum(backcome.values()) + betpass + backpass )
	betcome  = {4 : 0, 5 : 0, 6 : 0, 8 : 0, 9 : 0, 10 : 0}
	backcome = {4 : 0, 5 : 0, 6 : 0, 8 : 0, 9 : 0, 10 : 0}
	betpass  = 0
	backpass = 0
	onnum    = 0 # register for the number hit when game is first on 
	comelinetotal = 0        

def crapdata():
	global betcome,backcome,betpass,backpass,onnum,comelinetotal,comebox,gameison
	print """ you got the craps??
	betcome :      %s 
	backcome :     %s
	comebox:       %d
	betpass :      %d
	backpass :     %d
	onnum :        %d
	comelinetotal: %d
	gameison:      %s
	""" % (betcome , backcome, comebox, betpass ,backpass,onnum ,comelinetotal, gameison ) 


def dice():
	return [randint(1,6), randint(1,6)]

def askdice():
	ask = 0
	while 1:
		try:
			ask = int(raw_input('roll (90 for printout):'))
			if ask == 90:
				crapdata()
			elif ask > 12 or ask <= 0: 
				print "life with out luck is no life at all."
			else:
				break # dance
		except ValueError:
			print "error with input"
			continue
	return ask

##### 
# roll dice
# set on / off
# if off:
#	if bet on passline bet add bet to balance
#	bet passline, if passline not empty
# if on:
#	if # on ONBET, pay out pass and back pass, get off
#	if # on other come, pay out come bet
# 	move bet to come #, add 1 if 6 or 8
#	play come, if come empty 
#	back come bet on # if # empty
#	back pass bet on # if # empty 
#	
# 


hightry = balance
lowtry = balance

beforeonnum = 0

for x in range(500):
	
	bet_pass()
	
	
	print "-----------------"
	print "     Balance $%d " % balance
	print "-----------------"
	#tot = askdice() #int(raw_input('roll (90 for printout):'))
	#print 'dice comeing out'
	roll = dice()
	tot = sum(roll)
	print( str(roll) + ' -- ' + str(sum(roll)))
	
			
	# natrual
	if tot in natural:
		print "tot in natural"
		#if game is off
		if not gameison: #pay the passline
			print "betpass %d" % betpass
			won = (betpass * 2)
			print 'winner %d, pay the line $%d ' % (tot, won) 
			balance += won
			betpass = backpass = 0
		if gameison and tot == 7: 
			print '7, craps out'
			if comebox > 0:
				won = comebox * 2
				print 'win $%d on your comebox $%d' % (won, comebox)
				comebox = 0
				balance += won
				gameison = False
			crapsout()
	# craps
	elif tot in craps:
		print "tot in craps"
		if not gameison:
			# lose passline bet
			print "crap %d, loose passline bet" % tot
			betpass = 0
		if gameison:
			# lose come bet
			print "crap %d, loose comebox bet" % tot
			comebox = 0
	# come and odds
	elif tot in comes: # on
		print "tot in comes"
		# pay out
		beforeonnum = onnum
		if onnum == 0:
			print "GAME IS ON! dice on %d" % tot
			onnum = tot
			gameison = True
		elif gameison and tot == onnum:
			#pay out bet on ( betpass + backpass ) * 2
			won      = ( betpass * 2+ backpass * 3) 
			print 'you won $%d on your passline ' % won
			balance += won
			betpass  = backpass = 0
			onnum = 0
			beforeonnum = 0
			gameison = False # game is now off
		elif gameison and tot != onnum:
			#pay out bet betcome[tot] + backcome
			#print 'tot %d' % tot
			won = betcome[tot] * 2 + backcome[tot] * 3
			balance += won 
			if won > 0: print 'you won $%d on your come bet ' % won	
			comelinetotal -= betcome[tot] + backcome[tot]
			betcome[tot] = backcome[tot] = 0
		
		if beforeonnum == 0:
			back_pass()
			bet_comebox()
			#TODO: not sure of the exact logic here
			# return comebet if on, and there is a comebet on tot
			#if betcome[tot] > 0:
			#	pass
		else:
			bet_come(tot)
	#stats
	if otherdice.has_key(tot):
		otherdice[tot] += 1
	else:
		otherdice [tot] = 1
		
	if balance <= 0:
		print "You are out of money!"
		break
	
	if gameison and beforeonnum != 0:
		bet_comebox()
			
	#elif tot not in comes: #not quite right, need to handle field and other dice seprately
	#	# in field or else
	#	print 'some other dice :-('
	print "\n###\n\n"
	
	if hightry < balance: hightry = balance
	if lowtry > balance: lowtry = balance
print 'highest: %d, lowest:%d' % (hightry, lowtry)
print 'other dice %s' % otherdice
	
	
		
			

#print(dice())
#for x in range(10):
#    roll = dice()
#    tot = sum(roll)
#    print( str(roll) + ' -- ' + str(sum(roll)))
#    bet_pass()
#    if gameison and tot in comes:
#        print 'number has landed ' + str(roll) + ' -- ' + str(tot)
#	back_pass(gameison, tot)
#
#        gameison = roll
#    if ! gameison and tot in natural:
#        print 'pay the line'
#    if ! gameison and tot in natural:
#        print 'pay the line'
#    if gameison and tot == 7:
#        print 'craps '
#        gameison = 0


